<?php

class ValidateConfigure {

	var $validate_config;
	var $validate_msg_config;
	var $context;
	
	function ValidateConfigure($COMMON_VALIDATE_CONFIG, $context_obj=null) {
		
		$this->context = $context_obj;
		$validate_config_path = $COMMON_VALIDATE_CONFIG['validate_config_path'];
		$validate_msg_config_path = $COMMON_VALIDATE_CONFIG['validate_msg_config_path'];
		
		$json = "";
		
		$fp = fopen($validate_config_path, "r");
		if(!$fp) {
			echo 'can not read ' . $validate_config_path;
		} else {
			while(!feof($fp)) {
				$json .= fgets($fp);
			}
		}
		fclose($fp);
		
		$json = trim($json);
		$json = preg_replace("/^var[^\{]*/", "", $json);
		$json = str_replace(";", "", $json);
		
		$this->validate_config = json_decode($json, true);
		
		$json = "";
		
		$fp = fopen($validate_msg_config_path, "r");
		if(!$fp) {
			echo 'can not read ' . $validate_msg_config_path;
		} else {
			while(!feof($fp)) {
				$json .= fgets($fp);
			}
		}
		fclose($fp);
		
		$json = trim($json);
		$json = preg_replace("/^var[^\{]*/", "", $json);
		$json = str_replace(";", "", $json);
		
		$this->validate_msg_config = json_decode($json, true);
	}
	
	function get_value($id) {

		if(isset($this->context)) {
			return empty($this->context[$id])?null:$this->context[$id];
		}

		if(!empty($_POST[$id])) {
			return $_POST[$id];	
		} else {
			return $_GET[$id];	
		}
	}
	
	function get_validate_config() {
		
		return $this->validate_config;
	}
	
	function get_validate_msg_config() {
		
		return $this->validate_msg_config;
	}

}
?>
